<route lang="yaml">
meta:
  whiteList: true
  title: 登录
  constant: true
  layout: false
  </route>

<script lang="ts" setup name="LoginRegister">
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useI18n } from 'vue-i18n'

const emit = defineEmits(['update:formType'])
const { t } = useI18n()

const loading = ref(false)

// 注册
const registerFormRef = ref<FormInstance>()
const registerForm = ref({
  account: '',
  phone: '',
  captcha: '',
  password: '',
  checkPassword: ''
})
const registerRules = ref<FormRules>({
  account: [
    { required: true, trigger: 'blur', message: '请输入用户名' }
  ],
  phone: [
    { required: true, trigger: 'blur', message: '请输入手机号' }
  ],
  captcha: [
    { required: true, trigger: 'blur', message: '请输入验证码' }
  ],
  password: [
    { required: true, trigger: 'blur', message: '请输入密码' },
    { min: 6, max: 18, trigger: 'blur', message: '密码长度为6到18位' }
  ],
  checkPassword: [
    { required: true, trigger: 'blur', message: '请再次输入密码' },
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.value.password)
          callback(new Error('两次输入的密码不一致'))

        else
          callback()
      }
    }
  ]
})
function handleRegister() {
  ElMessage({
    message: '只提供页面,注册联调请自行扩展',
    type: 'warning'
  })
  registerFormRef.value && registerFormRef.value.validate(valid => {
    if (valid) {
      // 这里编写业务代码
    }
  })
}
</script>

<template>
  <el-form
    ref="registerFormRef"
    :model="registerForm"
    :rules="registerRules"
    class="login-form"
    auto-complete="on"
  >
    <div class="title-container">
      <h3 class="login-title">
        {{ t('app.createAccount') }}
      </h3>
    </div>
    <div>
      <el-form-item prop="account">
        <el-input v-model.trim="registerForm.account" placeholder="用户名" tabindex="1">
          <template #prefix>
            <el-icon>
              <IkSvgIcon name="iksvg_yonghu" />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="phone">
        <el-input
          v-model.trim="registerForm.phone"
          :placeholder="t('app.phone')"
          type="text"
          maxlength="11"
          tabindex="1"
        >
          <template #prefix>
            <el-icon>
              <IkSvgIcon name="iksvg_shouji" />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="captcha">
        <el-input v-model.trim="registerForm.captcha" :placeholder="t('app.code')" tabindex="2">
          <template #prefix>
            <el-icon>
              <IkSvgIcon name="iksvg_xiaoyanma" />
            </el-icon>
          </template>
          <template #append>
            <el-button>{{ t('action.send') }}{{ t('app.code') }}</el-button>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model.trim="registerForm.password"
          type="password"
          :placeholder="t('app.password')"
          tabindex="3"
          show-password
        >
          <template #prefix>
            <el-icon>
              <IkSvgIcon name="iksvg_suoping" />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="checkPassword">
        <el-input
          v-model.trim="registerForm.checkPassword"
          type="password"
          :placeholder="`${t('action.check')}${t('app.password')}`"
          tabindex="4"
          show-password
        >
          <template #prefix>
            <el-icon>
              <IkSvgIcon name="iksvg_suoping" />
            </el-icon>
          </template>
        </el-input>
      </el-form-item>
    </div>
    <el-button
      :loading="loading"
      type="primary"
      size="large"
      style="width: 100%; margin-top: 20px;"
      @click.prevent="handleRegister"
    >
      {{ t('action.register') }}
    </el-button>
    <div class="sub-link">
      <span class="text">{{ t('app.handAccount') }}?</span>
      <el-link type="primary" :underline="false" @click="emit('update:formType', 'login')">
        {{ t('app.goLogin') }}
      </el-link>
    </div>
  </el-form>
</template>

<style lang="scss" scoped>
@import './form';
</style>
